package me.sheimi.sgit;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.widget.TextView;

import uk.co.chrisjenx.calligraphy.CalligraphyConfig;
import uk.co.chrisjenx.calligraphy.CalligraphyContextWrapper;

/**
 *
 */
public class SGitApplication extends Application {

    private static Context mContext;
    private static Typeface TYPEFACE;

    @Override
    public void onCreate() {
        super.onCreate();
        if (null == TYPEFACE) {
            TYPEFACE = Typeface.createFromAsset(getAssets(), getString(R.string.default_font));
        }
        CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
            .setDefaultFontPath(getString(R.string.default_font))
            .setFontAttrId(R.attr.fontPath)
            .build()
        );
        mContext = getApplicationContext();
        setAppVersionPref();
    }

    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
    }

    public static Context getContext() {
        return mContext;
    }

    private void setAppVersionPref() {
        SharedPreferences sharedPreference = getSharedPreferences(
            getString(R.string.preference_file_key),
            Context.MODE_PRIVATE);
        String version = BuildConfig.VERSION_NAME;
        SharedPreferences.Editor editor = sharedPreference.edit();
        editor.putString(getString(R.string.preference_key_app_version), version);
        editor.commit();
    }

    public static void setTypeface(TextView... tvs) {
        if (null != TYPEFACE && null != tvs) {
            for (TextView tv : tvs)
                tv.setTypeface(TYPEFACE);
        }
    }

    public static void setTextsize(TextView... tvs) {
        if (null != tvs)
            for (TextView tv : tvs)
                tv.setTextSize(14.0f);
    }

    public static void setFont(TextView... tvs) {
        if (null != tvs) {
            for (TextView tv : tvs) {
                setTypeface(tv);
                setTextsize(tv);
            }
        }
    }

}
